<template>
  <!-- 日生产计划表表单弹窗 -->
  <el-dialog
          v-model="visibleDialog"
          :title="props.title"
          width="70%"
          draggable
          @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
      <el-col :span="12">
      <el-form-item label="生产计划编号" prop="producePlanCode">
      <el-input v-if="!props.isReadonly" v-model="formData.producePlanCode" placeholder="请输入生产计划编号" clearable />
  
      <span v-else>{{formData.producePlanCode}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="物料名称" prop="materialName">
      <el-input v-if="!props.isReadonly" v-model="formData.materialName" placeholder="请输入物料名称" clearable />
  
      <span v-else>{{formData.materialName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="日生产计划号" prop="planNum">
      <el-input v-if="!props.isReadonly" v-model="formData.planNum" placeholder="请输入日生产计划号" clearable />
  
      <span v-else>{{formData.planNum}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="生产日期" prop="planDate">
      <el-input v-if="!props.isReadonly" v-model="formData.planDate" placeholder="请输入生产日期" clearable />
  
      <span v-else>{{formData.planDate}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="数量" prop="planQuantity">
      <el-input v-if="!props.isReadonly" v-model="formData.planQuantity" placeholder="请输入数量" clearable />
  
      <span v-else>{{formData.planQuantity}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="计划状态" prop="planStatus">
    <el-select style="width: 100%;" v-if="!props.isReadonly" v-model="formData.planStatus" placeholder="请选择计划状态" clearable>
    <el-option
            v-for="item in dictionaryStore.filterDictList('ProduceOrderState')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
    />
  </el-select>
  
      <span v-else>{{ dictionaryStore.filterDictName('ProduceOrderState', formData.planStatus)}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="生产版本号" prop="productionVersionNumber">
      <el-input v-if="!props.isReadonly" v-model="formData.productionVersionNumber" placeholder="请输入生产版本号" clearable />
  
      <span v-else>{{formData.productionVersionNumber}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建时间" prop="createTime">
      <el-input v-if="!props.isReadonly" v-model="formData.createTime" placeholder="请输入创建时间" clearable />
  
      <span v-else>{{formData.createTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建人名" prop="createdUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.createdUserName" placeholder="请输入创建人名" clearable />
  
      <span v-else>{{formData.createdUserName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新时间" prop="lastUpdateTime">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdateTime" placeholder="请输入更新时间" clearable />
  
      <span v-else>{{formData.lastUpdateTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新人名" prop="lastUpdatedUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdatedUserName" placeholder="请输入更新人名" clearable />
  
      <span v-else>{{formData.lastUpdatedUserName}}</span>
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import request from "@/utils/request";
  import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
  const dictionaryStore = useDictStoreHook();

  defineOptions({
    name: "MESMESDAYPLANADDORUPDATE",
    inheritAttrs: false,
  });

  const emit = defineEmits(["reset-table", "change-visible"]);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    title: {
      type: String,
      default: () => {
        return "";
      },
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    isReadonly: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });

  const visibleDialog = ref(false);
  watch(
      () => props.visible,
      (newVal: boolean) => {
        visibleDialog.value = newVal;
        setTimeout(() => {
          formRef.value.clearValidate();
        }, 50);
      }
  );

  const rules = reactive({
    producePlanCode: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    materialName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    planNum: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    planDate: [
                        ],
    planQuantity: [
                        ],
    planStatus: [
                        ],
    productionVersionNumber: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    createTime: [
                        ],
    createdUserName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    lastUpdateTime: [
                        ],
    lastUpdatedUserName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
  });

  const formRef = ref(ElForm);

  let formData = reactive({
  id: "",
  producePlanCode: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  workSequence: "",
  planNum: "",
  planDate: "",
  planQuantity: "",
  planStatus: "",
  productionVersionNumber: "",
  externalCode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

  watch(
          () => props.data,
          (newVal: any) => {
            formData.id = ""
            if (newVal?.id) {
              const {  id,  producePlanCode,  organizationId,  organizationCode,  plantId,  plantCode,  materialId,  materialCode,  materialName,  workSequence,  planNum,  planDate,  planQuantity,  planStatus,  productionVersionNumber,  externalCode,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  deleted,  } = newVal;
              Object.assign(formData, {  id,  producePlanCode,  organizationId,  organizationCode,  plantId,  plantCode,  materialId,  materialCode,  materialName,  workSequence,  planNum,  planDate,  planQuantity,  planStatus,  productionVersionNumber,  externalCode,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  deleted,  });
            }
          }
  );

  /** 关闭表单弹窗 */
  function closeDialog() {
    visibleDialog.value = false;
    emit("change-visible", false);
    resetForm();
  }

  /** 重置表单 */
  function resetForm() {
    formRef.value.resetFields();
    formRef.value.clearValidate();
    formData.id = ""
    formData.producePlanCode = ""
    formData.organizationId = ""
    formData.organizationCode = ""
    formData.plantId = ""
    formData.plantCode = ""
    formData.materialId = ""
    formData.materialCode = ""
    formData.materialName = ""
    formData.workSequence = ""
    formData.planNum = ""
    formData.planDate = ""
    formData.planQuantity = ""
    formData.planStatus = ""
    formData.productionVersionNumber = ""
    formData.externalCode = ""
    formData.version = ""
    formData.remarks = ""
    formData.createTime = ""
    formData.createdUserId = ""
    formData.createdUserName = ""
    formData.lastUpdateTime = ""
    formData.lastUpdatedUserId = ""
    formData.lastUpdatedUserName = ""
    formData.deleted = ""
  }

  const loading = ref(false);

  /** 日生产计划表保存提交 */
  function handleSubmit() {
    formRef.value.validate(async (valid: any) => {
      if (valid) {
        loading.value = true;
        const text = formData.id ? "编辑" : "新增";
        await request({
          url: "/mes/mesdayplan/save",
          method: "post",
          data: formData,
        }).finally(() => {
          loading.value = false;
        });
        ElMessage.success( text +`成功`);
        closeDialog();
        emit("reset-table", formData.id);
      }
    });
  }


  // 组件销毁时调用
  onBeforeUnmount(() => {
    visibleDialog.value = false;
    emit("change-visible", false);
  });


</script>
